/*
 * Created on Aug 2, 2004
 */
package org.vectrics.company.web.struts;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.vectrics.SystemException;
import org.vectrics.company.Company;
import org.vectrics.company.CompanyPerson;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;
import org.vectrics.user.web.struts.UserGroupHelper;


/**
 * @author mmoore
 */
public class SaveCompanyPersonGroupAction extends VectricsStrutsAction {
	private transient final Logger log = Logger.getLogger(SaveCompanyPersonGroupAction.class);
	/* 
	 * @see org.vectrics.company.web.struts.RequestFormAction#createForm()
	 */
	protected ActionForm createForm() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.vectrics.company.web.struts.RequestFormAction#fillForm(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected String doExecute(VectricsActionData actionData)	throws JspException {
		CompanyPersonGroupForm form = (CompanyPersonGroupForm)actionData.getForm();
		CompanyPerson companyPerson = null;
		Company company = null;
		Person person = null;
		
		company = CoreServiceLocator.getCompanyService().findCompany(new Long(form.getCompanyId()));
		person = CoreServiceLocator.getPersonService().findPersonById(new Long(form.getPersonId()));
		
		companyPerson = CoreServiceLocator.getCompanyService().findCompanyPerson(company, person);
		
		
		if (companyPerson == null) {
			throw new SystemException("CompanyPerson is null");
		}
		
		UserGroupHelper helper = new UserGroupHelper();
		helper.saveGroups(actionData, person, company.getDomain());
		
		actionData.getRequest().setAttribute("companyPersonId", (String)companyPerson.getId().toString());

		return "success";
	}

}
